﻿using System.Windows.Threading;

namespace Super.Hmi.Utils.EventAggregator
{
    // 事件订阅信息
    public class Subscription
    {
        public Type EventType { get; } // 订阅的事件类型
        public Delegate Handler { get; } // 订阅的事件处理程序
        public Predicate<object> Filter { get; } // 过滤条件
        public bool KeepSubscriberReferenceAlive { get; } // 是否强引用
        public DispatcherPriority DispatcherPriority { get; } // 处理程序在UI线程上执行时的优先级
        public WeakReference SubscriberReference { get; } // 订阅者的弱引用
        /// <summary>
        /// 订阅事件的构造函数
        /// </summary>
        /// <param name="eventType">订阅的事件类型</param>
        /// <param name="handler">订阅的事件</param>
        /// <param name="filter">过滤条件</param>
        /// <param name="keepSubscriberReferenceAlive">是否强引用 【强引用垃圾回收不会自动回收 需手动回收】</param>
        /// <param name="dispatcherPriority">处理程序在UI线程上执行时的优先级</param>
        /// <param name="subscriber">订阅者对象</param>
        public Subscription(Type eventType, Delegate handler, Predicate<object> filter = null, bool keepSubscriberReferenceAlive = false, DispatcherPriority dispatcherPriority = DispatcherPriority.Normal, object subscriber = null)
        {
            EventType = eventType;
            Handler = handler;
            Filter = filter;
            KeepSubscriberReferenceAlive = keepSubscriberReferenceAlive;
            DispatcherPriority = dispatcherPriority;
            if (subscriber != null)
            {
                SubscriberReference = new WeakReference(subscriber);
            }
        }
    }
}
